/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.widget.fraction;

import com.viewpagerindicator.TabPageIndicator;
import me.panpf.sketch.adapter.pager.ViewArrayPagerAdapter;
import me.panpf.sketch.sample.AssetImage;
import me.panpf.sketch.sample.ResourceTable;
import me.panpf.sketch.sample.listener.PageChangeListener;
import me.panpf.sketch.sample.widget.page.ImageOrientationTestView;
import me.panpf.sketch.util.ExifInterface;
import me.panpf.sketch.util.Utils;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;

import java.util.ArrayList;
import java.util.List;

/**
 * ImageOrientationTestFraction
 */
public class ImageOrientationTestFraction extends Fraction {
    private final List<Component> mPageViewList = new ArrayList<>();
    private TabPageIndicator mTabList;

    private static final int DEFAULE_INDEX = 0;
    private static final String[] TAB_TITLES = new String[]{"ROTATE_90", "ROTATE_180",
            "ROTATE_270", "FLIP_HORIZONTAL", "TRANSPOSE", "FLIP_VERTICAL", "TRANSVERSE"};
    private static final int[] TAB_EXIF_TAGS = new int[]{ExifInterface.ORIENTATION_ROTATE_90, ExifInterface.ORIENTATION_ROTATE_180,
            ExifInterface.ORIENTATION_ROTATE_270, ExifInterface.ORIENTATION_FLIP_HORIZONTAL,
            ExifInterface.ORIENTATION_TRANSPOSE, ExifInterface.ORIENTATION_FLIP_VERTICAL, ExifInterface.ORIENTATION_TRANSVERSE};
    private static final String[] TAB_FILE_PATHS = new String[]{AssetImage.ROTATE_90, AssetImage.ROTATE_180,
            AssetImage.ROTATE_270, AssetImage.FLIP_HORIZONTAL,
            AssetImage.TRANSPOSE, AssetImage.FLIP_VERTICAL, AssetImage.TRANSVERSE};

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_image_orientation_test_fraction, container, false);
        return component;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        mTabList = (TabPageIndicator) getComponent().findComponentById(ResourceTable.Id_tab_list);
        PageSlider pageSlider = (PageSlider) getComponent().findComponentById(ResourceTable.Id_pager_slider);

        mPageViewList.clear();
        for (int i = 0; i < TAB_EXIF_TAGS.length; i++) {
            mPageViewList.add(new ImageOrientationTestView(getFractionAbility().getContext(), TAB_EXIF_TAGS[i], TAB_FILE_PATHS[i]));
        }

        ViewArrayPagerAdapter adapter = new ViewArrayPagerAdapter(mPageViewList, TAB_TITLES);
        pageSlider.setProvider(adapter);
        pageSlider.addPageChangedListener(new PageChangeListener(mTabList));

        mTabList.setTabTextSize(Utils.vp2px(getFractionAbility().getContext(), "12fp"));
        mTabList.setTabMargin(20);
        mTabList.setOrientation(Component.HORIZONTAL);
        mTabList.setCentralScrollMode(true);
        mTabList.setTabTextAlignment(TextAlignment.CENTER);
        mTabList.setTabTextColors(getFractionAbility().getContext().getColor(ResourceTable.Color_color_tab_normal),
                Color.WHITE.getValue());
        mTabList.setSelectedTabIndicatorHeight(0);
        mTabList.setViewPager(pageSlider);
        mTabList.setFixedMode(false);
        // 设置默认选择的页签项
        mTabList.selectTab(mTabList.getTabAt(DEFAULE_INDEX));
    }

}